package com.xc.ezplan.admin.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xc.ezplan.admin.entity.AdminMenu;
import com.xc.ezplan.admin.entity.AdminRole;
import com.xc.ezplan.admin.entity.AdminRolesMenus;
import com.xc.ezplan.admin.entity.AdminUsersRoles;
import com.xc.ezplan.admin.mapper.AdminMenuMapper;
import com.xc.ezplan.admin.mapper.AdminRoleMapper;
import com.xc.ezplan.admin.mapper.AdminRolesMenusMapper;
import com.xc.ezplan.admin.mapper.AdminUsersRolesMapper;
import com.xc.ezplan.admin.service.AdminPurviewService;
import lombok.RequiredArgsConstructor;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @author lixingchen
 * @date 2022/12/20
 * @description 用户权限实现类
 */
@Service
@Primary
@RequiredArgsConstructor
@CacheConfig(cacheNames = "purview")
public class AdminPurviewServiceImpl implements AdminPurviewService {


    private final AdminMenuMapper adminMenuMapper;

    private final AdminRolesMenusMapper adminRolesMenusMapper;

    private final AdminUsersRolesMapper adminUsersRolesMapper;

    private final AdminRoleMapper adminRoleMapper;

    @Override
    @Cacheable(key = "'permission:' + #p0")
    public List<String> getPermissionsByUser(Long userId) {
        // 获取用户ID
        if (userId ==null){
            userId = StpUtil.getLoginIdAsLong();
        }
        List<String> permissionList = new ArrayList<>();
        // 老板拥有全部权限
        if (userId.equals(1L)) {
            List<AdminMenu> adminMenus = adminMenuMapper.selectList(null);
            adminMenus.forEach(menu -> permissionList.add(menu.getPermission()));
            return permissionList;
        }
        List<Long> roleIds = adminUsersRolesMapper
                .selectList(new QueryWrapper<AdminUsersRoles>().lambda().eq(AdminUsersRoles::getUserId, userId))
                .stream().map(AdminUsersRoles::getRoleId).collect(Collectors.toList());
        Set<Long> menuIds = adminRolesMenusMapper
                .selectList(new QueryWrapper<AdminRolesMenus>().lambda().in(AdminRolesMenus::getRoleId, roleIds))
                .stream().map(AdminRolesMenus::getMenuId).collect(Collectors.toSet());
        adminMenuMapper.selectList(new QueryWrapper<AdminMenu>().lambda().in(AdminMenu::getId, menuIds))
                .forEach(menu -> permissionList.add(menu.getPermission()));
        return permissionList;
    }

    @Override
    @Cacheable(key = "'role:' + #p0")
    public List<String> getRolesByUser(Long userId) {
        // 获取用户ID
        if (userId ==null){
            userId = StpUtil.getLoginIdAsLong();
        }
        List<String> roleList = new ArrayList<>();
        // 老板拥有全部角色
        if (userId.equals(1L)) {
            List<AdminRole> adminRoles = adminRoleMapper.selectList(null);
            adminRoles.forEach(role -> roleList.add(role.getIdentity()));
            return roleList;
        }
        List<Long> roleIds = adminUsersRolesMapper
                .selectList(new QueryWrapper<AdminUsersRoles>().lambda().eq(AdminUsersRoles::getUserId, userId))
                .stream().map(AdminUsersRoles::getRoleId).collect(Collectors.toList());
        adminRoleMapper.selectList(new QueryWrapper<AdminRole>().lambda().in(AdminRole::getId, roleIds))
                .forEach(role -> roleList.add(role.getIdentity()));
        return roleList;
    }
}
